package ar.com.e2solver.solver;

import ar.com.e2solver.model.Piece;
import ar.com.e2solver.model.Puzzle;

public class Individual {

	private Puzzle thePuzzle;
	private int fitnessValue;
	
	public Individual(Puzzle puzzle){
		this.thePuzzle = puzzle;
		this.fitnessValue = getFitness(puzzle); 
	}
	
	private int getFitness(Puzzle puzzle) {
		int fitness = 0;
		for(int i = 0; i < getPuzzle().getRowsNumber(); i++){
			for(int j = 0; j < getPuzzle().getColumnNumbers(); j++){
				Piece piece = puzzle.getPieceAt(i, j);
				
				if(i < puzzle.getRowsNumber() - 1 && piece.getBottom().equals(puzzle.getPieceAt(i+1, j).getTop())){
					fitness++;
				}
				
				if(j < puzzle.getColumnNumbers() - 1 && piece.getRight().equals(puzzle.getPieceAt(i, j+1).getLeft())){
					fitness++;
				}
			}
		}
			
		return fitness;
	}

	public Puzzle getPuzzle() {
		return thePuzzle;
	}

	public int getFitnessValue() {
		return fitnessValue;
	}	
}
